// pages/mine/personalInformation/authenticationData/locationHospital/locationHospital.js
// 引用百度地图微信小程序JSAPI模块 
var bmap = require('../../../../../sdk/bmap-wx.js')
var loclcache = require('../../../../../utils/cache.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logsList: [
      {id:1,name:2},
      { id: 1, name: 2 },
    ],
    sugData:[],
    city: [],
    // value: ['广东省', '深圳市', '福田区'],
    value: {
      name:'深圳市',
      lat: 22.53332,
      lng: 113.93041
    },
    showPicker: false,
    hospital:'',
    multiIndex:0,
    hospitalPage:0,
    loading:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.searchNearby('医院',0)
    this.citySearch()
    wx.getLocation({
      type: 'gcj02',
      success: (res)=>{
        console.log(res)
      },
      fail: (rp)=>{
        console.log(rp)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 选择地区
   */
  chooseDistrict: function () {
    let self = this
    self.setData({
      showPicker: !self.data.showPicker
    })
  },
  //城市搜索
  citySearch:function(){
    let postUrl = wx.envConfig.host + '/region/findByLevelType?cookiez=' + wx.getStorageSync('cookiez')
    wx.request({
      url: postUrl,
      method: 'POST',
      success: (re) => {
        if (re.data.error_code === 'Y10000') {
          this.setData({
            city: re.data.datas
          })
        }
      },
      fail: function (res) { }
    })
  },
  /**
   * 点击放大镜搜索
   */
  search: function () {

  },
  /**
   * 取消按钮
   */
  cancel: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 绑定input输入
   * @param {event}  e - event事件包含了输入内容
   * */
  bindKeyInput: function (e) {
    this.data.hospital = e.detail.value
    this.setData({
      hospital: e.detail.value,
      hospitalPage:0
    })
    this.searchNearby(e.detail.value,0)
  },
  /**
   * 搜索附近
   * @param {String}  string - 需要搜索的字符串
   * @param {Number}  p - 页数
   * */
  searchNearby: function (string,p) {
    var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'WPZ957HCK4XfLAX85HPsPgnlev5YVRme'
    });
    var fail = function (data) {
      wx.hideLoading()
      console.log(data)
    };
    var success = function (data) {
      wx.hideLoading()
      console.log(data)
      if (p!=0){
        this.data.sugData.push.apply(this.data.sugData, data.originalData.results);
        this.setData({
          sugData: this.data.sugData,
          loading: false
        });
      }else{
        this.setData({
          sugData: data.originalData.results,
          loading: false
        })
      }
    }
    // 发起suggestion检索请求 
    wx.showLoading({
      title:'加载中'
    })
    BMap.search({
      query: string,
      region: this.data.value.name,
      city_limit: true,
      fail: fail,
      success: success.bind(this),
      page_size:20,
      page_num: p
    });
  },
  /**
   * 城市选中回调
   * */
  selectChange:function(e){
    console.log(e)
    this.setData({
      value: e.currentTarget.dataset.item,
      hospitalPage: 0
    })
    this.chooseDistrict()
    this.searchNearby('医院',0)
  },
  /**
   * 医院下拉刷新
   * */
  morehospital:function(e){
    console.log(e)
    if (!this.data.loading){
      this.setData({
        hospitalPage: this.data.hospitalPage + 1,
        loading: true
      })
      this.searchNearby(this.data.hospital||'医院', this.data.hospitalPage)
    }
  },
  /**
   * 选中医院
   */
  selectLogsItem: function (e) {
    console.log(e, loclcache)
    loclcache.default.set('hospitalobj', e.currentTarget.dataset.item)
    console.log(loclcache.default.get('hospitalobj'))
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 使用此医院
   * */
  usehospital:function(){
    loclcache.default.set('hospitalobj', {
      city: this.data.value.name,
      name: this.data.hospital,
      location:{
        lat: this.data.value.lat,
        lng: this.data.value.lng
      },
      address: this.data.hospital
    })
    console.log(loclcache.default.get('hospitalobj'))
    wx.navigateBack({
      delta: 1
    })
  }
})